<?php
/**
 *
文件上传控制器。
 *
 **/

namespace app\admin\controller;


class Upload extends Comm
{
    public function index($type = null)
    {

    }

	// 单图上传
    public function uploadpic()
    {
        $Img = input('Img');
        $Path = null;
        if (request()->file('img')) {
            $Img = $this->saveimg(request()->file('img'));
        }
        $BackCall = input('BackCall');
        $Width = input('Width');
        $Height = input('Height');
        if (!$BackCall) {
            $Width = $_POST['BackCall'];
        }
        if (!$Width) {
            $Width = $_POST['Width'];
        }
        if (!$Height) {
            $Width = $_POST['Height'];
        }
        $this->assign('Width', $Width);
        $this->assign('BackCall', $BackCall);
        $this->assign('Img', $Img);
        $this->assign('Height', $Height);
		return $this->fetch('Uploadpic');
    }

    private function saveimg($files)
    {
		$rootPath = '/Public/';
		$savePath = 'attached/'.date('Y')."/".date('m')."/";
		$info = $files->rule(function($file){$savePath = '/'.date('Y')."/".date('m')."/";return $savePath . uniqid();})->validate(['size'=>3145728,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . $rootPath . DS . 'attached');
		if($info){
			// 成功上传后 获取上传信息
			// 输出 jpg
			//echo $info->getExtension();
			// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
			//echo $info->getSaveName();
			// 输出 42a79759f284b767dfcb2a0197904287.jpg
			//echo $info->getFilename(); 
			//echo $info->getRealPath();die;
			$ImgStr = $rootPath.$savePath.$info->getFilename();
			//print_R($info);die;
            return $ImgStr;
		}else{
			// 上传失败获取错误信息
			$error= $files->getError();
			echo "<script>alert('{$error}')</script>";
		}
    }

	// 多图上传
    public function batchpic()
    {
        $ImgStr = input('Img');
        $ImgStr = trim($ImgStr, '|');
        $Img = array();
        if (strlen($ImgStr) > 1) {
            $Img = explode('|', $ImgStr);
        }
        $Path = null;
        $newImg = array();
        $newImgStr = null;
		$files = request()->file('uploadimg');
		
        if ($files) {
            $newImgStr = $this->saveimgd($files);

            if ($newImgStr) {
                $newImg = explode('|', $newImgStr);
            }
        }
        $Img = array_merge($Img,$newImg);
        $ImgStr = implode("|", $Img);
        $BackCall = input('BackCall');
        $Width = input('Width');
        $Height = input('Height');
        if (!$BackCall) {
            $Width = $_POST['BackCall'];
        }
        if (!$Width) {
            $Width = $_POST['Width'];
        }
        if (!$Height) {
            $Width = $_POST['Height'];
        }
        $this->assign('Width', $Width);
        $this->assign('BackCall', $BackCall);
        $this->assign('ImgStr', $ImgStr);
        $this->assign('Img', $Img);
        $this->assign('Height', $Height);
		return $this->fetch('Batchpic');
    }
	
    private function saveimgd($files)
    {
		$rootPath = '/Public/';
		$savePath = 'attached/'.date('Y')."/".date('m')."/";
		foreach($files as $keys){
			$infos = $keys->rule(function($file){$savePath = '/'.date('Y')."/".date('m')."/";return $savePath . uniqid();})->validate(['size'=>3145728,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . $rootPath . DS . 'attached');
			if($infos){
				$filePath[] = $rootPath.$savePath.$infos->getFilename();
				$ImgStr = implode("|", $filePath);
			}else{
				// 上传失败获取错误信息
				$error= $keys->getError();
				echo "<script>alert('{$error}')</script>";
			}
		}
		return $ImgStr;
    }
	
	
	//编辑器图片上传
	public function picupdate(){
		$file = request()->file('FileName');
		$info = $file->validate(['size'=>200000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . DS . 'public/wangEditor');
		if ($info) {
			$path = DS . 'public/wangEditor' . DS . $info->getSaveName();
			echo str_replace("\\", "/", $path);
		}else{
			$error= $file->getError();
			$this->error($error);	
		}	
	}
	
	// LAYUI单图上传
	public function picup(){
		$file = request()->file('FileName');
		$rootPath = '/Public/';
		$savePath = 'attached/'.date('Y')."/".date('m')."/";
		$info = $file->rule(function($file){$savePath = '/'.date('Y')."/".date('m')."/";return $savePath . uniqid();})->validate(['size'=>3145728,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . $rootPath . DS . 'attached');
		if ($info) {
			$path = $rootPath.$savePath.$info->getFilename();
			$pcc = str_replace("\\", "/", $path);
			$this->success('上传成功！',null,$pcc);
		}else{
			$error= $file->getError();
			$this->error($error);	
		}	
	}	
	
	
}
